<!--分页条组件-->
<template>
  <el-row :class="align" class="pagination-container" v-if="!hidden">
    <el-pagination
        :background="background"
        :current-page.sync="currentPage"
        :page-size.sync="pageSize"
        :layout="layout"
        :page-sizes="pageSizes"
        :pager-count="pagerCount"
        :total="total"
        v-bind="$attrs"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </el-row>
</template>

<script>
import { scrollTo } from '@/utils/scroll-to'

export default {
  name: 'Pagination',
  props: {
    /**
     * 总数
     */
    total: {
      required: true,
      type: Number
    },
    /**
     * 当前页码
     */
    page: {
      type: Number,
      default: 1
    },
    /**
     * 每页条数
     */
    limit: {
      type: Number,
      default: 20
    },
    /**
     * 每页可显示条数
     */
    pageSizes: {
      type: Array,
      default() {
        return [10, 20, 50, 200]
      }
    },
    /**
     * 移动端页码按钮的数量端默认值
     */
    pagerCount: {
      type: Number,
      default: document.body.clientWidth < 992 ? 4 : 7
    },
    /**
     * 功能
     */
    layout: {
      type: String,
      default: 'total, sizes, prev, pager, next, jumper'
    },
    /**
     * 是否为分页按钮添加背景色
     */
    background: {
      type: Boolean,
      default: true
    },
    /**
     * 自动滚动
     */
    autoScroll: {
      type: Boolean,
      default: true
    },
    /**
     * 是否隐藏分页条
     */
    hidden: {
      type: Boolean,
      default: false
    },
    /**
     * 对齐方式 left right center
     */
    align:{
      type: String,
      default: "right"
    }
  },
  data() {
    return {}
  },
  computed: {
    currentPage: {
      get() {
        return this.page
      },
      set(val) {
        this.$emit('update:page', val)
      }
    },
    pageSize: {
      get() {
        return this.limit
      },
      set(val) {
        this.$emit('update:limit', val)
      }
    }
  },
  methods: {
    /**
     * 当前页显示数据量改变
     * @param val
     */
    handleSizeChange(val) {
      if (this.currentPage * val > this.total) {
        this.currentPage = 1
      }
      this.$emit('pagination', { page: this.currentPage, limit: val })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    },
    /**
     * 页码改变
     * @param val
     */
    handleCurrentChange(val) {
      this.$emit('pagination', { page: val, limit: this.pageSize })
      if (this.autoScroll) {
        scrollTo(0, 800)
      }
    }
  }
}
</script>

<style scoped>
.pagination-container {
  width: 100%;
  display: flex;
  align-items: center;
  padding-top: 5px;
}

.left{
  justify-content: left;
}

.center{
  justify-content: center;
}

.right{
  justify-content: right;
}
</style>
